package com.itheima.imclient100.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.itheima.imclient100.R;
import com.itheima.imclient100.adapter.ContactAdapter;
import com.itheima.imclient100.event.ContactChangedEvent;
import com.itheima.imclient100.presenter.ContactPresenter;
import com.itheima.imclient100.presenter.impl.ContactPresenterImpl;
import com.itheima.imclient100.utils.SPUtils;
import com.itheima.imclient100.utils.ToastUtils;
import com.itheima.imclient100.widget.ContactLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by fullcircle on 2017/7/2.
 *  这个是联系人页面
 *
 */

public class ContactFragment extends BaseFragment implements ContactView {
    private ContactPresenter presenter = null;
    private ContactAdapter adapter;
    private ContactLayout contactLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contact,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        contactLayout = (ContactLayout) view.findViewById(R.id.contact_layout);
        presenter = new ContactPresenterImpl(this);
        //初始化查询联系人数据
        presenter.initContacts(SPUtils.getUsername(getContext()));
    }



    @Override
    public void onInitContacts(List<String> contacts) {
        adapter = new ContactAdapter(contacts);
        adapter.setListener(new ContactAdapter.MyClickListener() {
            @Override
            public void onClick(String username) {
                Intent intent = new Intent(getActivity(),ChatActivity.class);
                intent.putExtra("username",username);
                startActivity(intent);
            }

            @Override
            public void onLongClick(final String username) {
                Snackbar.make(contactLayout,"真的要删除好友:"+username+"吗?",Snackbar.LENGTH_SHORT)
                        .setAction("删除", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                presenter.deleteContact(username);
                            }
                        }).show();
            }
        });
        contactLayout.setAdapter(adapter);
        contactLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //联网获取最新的联系人数据
                presenter.updateContacts(SPUtils.getUsername(getContext()));
            }
        });


    }

    @Override
    public void onUpdateContacts(boolean isSuccess, List<String> contacts, String errMsg) {
        //隐藏刷新的转动图标
        contactLayout.setRefreshing(false);
        if(isSuccess){
            //利用联网获取的最新数据 更新适配器中的集合
            adapter.setContact(contacts);
            //适配器通知刷新界面
            adapter.notifyDataSetChanged();
        }else{
            ToastUtils.showToast(getContext(),errMsg);
        }
    }

    @Override
    public void onDeleteContact(boolean isSuccess, String errMsg) {
            if(isSuccess){
                ToastUtils.showToast(getContext(),"删除成功");
            }else{
                ToastUtils.showToast(getContext(),"删除失败"+errMsg);
            }
    }

    //这个eventbus又是哪里的。。
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void contactChanged(ContactChangedEvent event){
        //收到联系人变化的消息之后 刷新界面
        presenter.updateContacts(SPUtils.getUsername(getContext()));
    }


    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
